﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using DevExpress.XtraBars.Alerter;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;
using System.Linq;

namespace QuangNgai.nhaplieu
{
    public partial class frmChiTietVB : DevExpress.XtraEditors.XtraForm
    {

        private string[] fileNames;
        public QuangNgai.LTQNDataSet.VanBanRow VB { get; set; }

        public frmChiTietVB()
        {
            InitializeComponent();
        }
        public frmChiTietVB(LTQNDataSet.VanBanRow VB)
        {
            this.VB = VB;
            InitializeComponent();
        }

        public GridView GvChiTietVB
        {
            get { return gvChiTietVB; }
        }

        private void frmChiTietVB_Load(object sender, EventArgs e)
        {
            this.vanBanFileTableAdapter.FillByVBID(lTQNDataSet.VanBanFile, this.VB.VBID);
            this.vbfieldsTableAdapter.Fill(this.lTQNDataSet.Vbfields);
            //this.vanBanDetailTableAdapter.Fill(this.lTQNDataSet.VanBanDetail);
            vanBanDetailTableAdapter.FillByVBID(lTQNDataSet.VanBanDetail, this.VB.VBID);
        }

        private void gvChiTietVB_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            vanBanDetailBindingSource.EndEdit();
            vanBanDetailTableAdapter.Update(lTQNDataSet.VanBanDetail);
        }

        public void gvChiTietVB_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                tab2.Select();
                tab1.Select();
                gcChiTietVB.Focus();
                gvChiTietVB_RowUpdated(null, null);
            }
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDlg.ShowDialog();

            fileNames = openFileDlg.FileNames;

            lbFiles.Items.Clear();
            foreach (var f in openFileDlg.FileNames)
            {
                lbFiles.Items.Add(f);
            }
        }

        private void btnSavePath_Click(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string folderName = @"\_Attachments\HS";
            string targetPath = Path.GetDirectoryName(Application.ExecutablePath) + folderName + VB.HSID;
            string fileName = "";
            string destFile = "";

            if (lbFiles.Items.Count == 0)
            {
                MessageBox.Show("Chưa chọn file", "Chưa chọn file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }


            foreach (var f in fileNames)
            {
                if (Directory.Exists(Path.GetDirectoryName(f)))
                {
                    //fileName = Path.GetFileName(f);
                    fileName = "F" + VB.HSID + "-" + VB.VBID + "-S" + Guid.NewGuid().ToString() + Path.GetExtension(f);
                    Console.WriteLine(fileName.Length);
                    destFile = Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(f, destFile, true);

                    //TOTO insert : error file ==null
                    //var file = vanBanFileBindingSource.AddNew() as LTQNDataSet.VanBanFileRow;
                    var file = new VanBanFile();
                    file.FileID = Path.GetFileNameWithoutExtension(destFile);
                    file.FileOldName = Path.GetFileNameWithoutExtension(f);
                    file.FileNewName = Path.GetFileName(destFile);
                    FileInfo fInfo = new FileInfo(f);
                    file.FileSize = fInfo.Length;
                    file.FileType = Path.GetExtension(f);
                    file.UploadDate = DateTime.Now;
                    file.Path = targetPath;

                    file.VBID = VB.VBID;

                    LTQNLinQDataContext db = new LTQNLinQDataContext();
                    db.VanBanFiles.InsertOnSubmit(file);
                    db.SubmitChanges();
                    //vanBanDetailBindingSource.EndEdit();
                    //vanBanFileTableAdapter.Update(lTQNDataSet.VanBanFile);
                    vanBanFileTableAdapter.FillByVBID(lTQNDataSet.VanBanFile, VB.VBID);
                }
            }

            MessageBox.Show("Lưu thành công", "Lưu thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        private void btnTuDien_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmChiTietVBLKTD f = new frmChiTietVBLKTD(this);
            f.Show();
        }

        private void gvChiTietVB_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            ColumnView view = sender as ColumnView;
            var currRow = view.GetFocusedDataRow() as LTQNDataSet.VanBanDetailRow;
            gvChiTietVB.Tag = currRow;
        }

        private void gvFile_RowUpdated(object sender, RowObjectEventArgs e)
        {
            vanBanFileBindingSource.EndEdit();
            vanBanFileTableAdapter.Update(lTQNDataSet.VanBanFile);
        }

        private void gvFile_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose.Focus();
                gcChiTietVB.Focus();
                gvFile_RowUpdated(null, null);
            }
        }

        private void gcFile_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            
            //var navC = sender as GridControlNavigator;
            //var currGridControl = navC.NavigatableControl as GridControl;
            //var currGridView = currGridControl.FocusedView;
            var row = (vanBanFileBindingSource.Current as DataRowView).Row as LTQNDataSet.VanBanFileRow;
            LTQNLinQDataContext db = new LTQNLinQDataContext();
            var file = (from f in db.VanBanFiles
                        where f.FileID == row.FileID
                        select f).First();
            if (e.Button.ButtonType == NavigatorButtonType.Remove)
            {
                bool isFileExisted;
                try
                {
                    isFileExisted = _formUtil.DeleteVanBanFile(file);
                    string message = "Dữ liệu về file \n" + file.FileNewName + " \nđã được xóa\n";
                    if (!isFileExisted)
                    {
                        message += "(File không tồn tại)";
                    }
                    MessageBox.Show(message, "Xóa thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                
            }

        }
    }
}